<template>
	<view @click="close" class="mask">
		<view class="x-popup">
			<view class="popup-title">赠送对方礼物
				<view class="xiaoju-popup-close" @click="close()">关闭</view>
			</view>
			<scroll-view @click.stop class="popup-wrap" scroll-y="true" :show-crollbar="true" scroll-with-animation="true">
				<view class="x-list">
					<view v-for="(l,i) in gift" :key="i" @click.stop="gift_active=i" class="list-cell"
						:class="{'list-active':i==gift_active}">
						<image class="list-icon" :src="l.Icon?l.Icon:'/static/img/icon_bbt.png'" mode=""></image>
						<view v-if="i!=gift_active" class="list-name"> {{l.Name}} </view>
						<view class="list-price"> ￥{{l.Price}} </view>
						<!-- <view v-if="i==gift_active" @click="giftEvent" class="list-btn"> 赠送 </view> -->

						<view v-if="i==gift_active" class="list-btn">
							<view class="add" @click.stop="gift_num_reduce(l)">-</view>
							<view class="count">
								<input type="text" v-model="l.Num" />
							</view>
							<view class="add" @click.stop="gift_num_plus(l)">+</view>

						</view>
					</view>
				</view>
			</scroll-view>
			<view class="popup-btn" @click.stop="giftEvent">赠送</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				gift: [], //礼物数组
				gift_active: -1, //当前选中礼物
				sel_community: '',
			};
		},
		onLoad(e) {
			this.sel_community = JSON.parse(e.item)
			this.GetGift();
		},
		methods: {
			// 获取礼物列表
			async GetGift() {
				let res = await this.$api.post('Friend/GetGift');
				this.gift = res.data || [];
			},
			//礼数数减
			gift_num_reduce(l) {
				if (l.Num >= 1) {
					l.Num--
				}
			},
			//礼物数加
			gift_num_plus(l) {
				if (l.Num >= 0) {
					l.Num++
				}
			},
			// 赠送礼物的相关逻辑
			async giftEvent() {

				// 当前选中礼物
				const l = this.gift[this.gift_active]

				const gift_s_price = l.Price;
				const gift_price = l.Price * l.Num;


				const MessageId = Math.random().toString().substr(2, 10);

				let res_gift = await this.$api.post('Friend/SendGift', {
					Price: gift_price,
					GiftID: l.ID,
					GiftName: l.Name,
					GiftQty: l.Num,
					ToUserID: this.sel_community.UserID,
					EventID: this.sel_community.ID,
					SendType: '球友圈赠送',
					MessageId: MessageId
				});


				// 支付请求
				let pres = await this.$api.post('Payment/PayV2', {
					total_fee: gift_price,
					EventType: '赠送礼物',
					EventID: JSON.stringify(res_gift.data),
					body: l.Name,
					// #ifdef MP-WEIXIN
					PayChannel: "weixin",
					// #endif
					// #ifdef APP
					PayChannel: "app"
					// #endif
				});
				// 已余额支付成功
				if (pres.data == 0) {
					this.close()
					uni.$emit('giftEvent', {
						Num: l.Num,
						gift_price: gift_price,
						Name: l.Name,
						MessageId: MessageId,
						Icon: l.Icon
					})
					return;
				}

				// 支付成功返回的内容
				let payObj = JSON.parse(pres.data);
				let _this = this;
				// 调用uni支付API
				uni.requestPayment({
					// 服务提供商
					provider: 'wxpay',
					// #ifdef APP
					orderInfo: payObj,
					// #endif
					// #ifdef MP-WEIXIN
					timeStamp: payObj.timeStamp,
					nonceStr: payObj.nonceStr,
					package: payObj.package,
					signType: payObj.signType,
					paySign: payObj.paySign,
					// #endif
					success(res) {
						_this.close()
						uni.$emit('giftEvent', {
							Num: l.Num,
							gift_price: gift_price,
							Name: l.Name,
							MessageId: MessageId,
							Icon: l.Icon
						})
					},
					fail(err) {
						_this.$pv.msg('支付失败');
					}
				});
			},
			onClick(e) {},
			close() {
				uni.navigateBack();
			}
		}
	};
</script>

<style lang="scss">
	page {
		background: transparent;
	}

	.mask {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
		background-color: rgba(0, 0, 0, 0);
	}

	.x-popup {
		background-color: #ffffff;
		width: 650rpx;
		border-radius: 20rpx;
		padding-bottom: 20rpx;

		.popup-title {
			padding: 20rpx 0 10rpx;
			text-align: center;
			font-size: 32rpx;
			position: relative;
			color: #333;

			.xiaoju-popup-close {
				font-size: 24rpx;
				position: absolute;
				right: 50rpx;
				top: 10rpx;
				color: #333;
			}
		}

		.popup-wrap {
			max-height: 800rpx;
			min-height: 300rpx;
			padding-top: 10rpx;
			padding-bottom: 30rpx;

			.popup-cell {
				padding: 8rpx 40rpx;
				font-size: 30rpx;
				line-height: 44rpx;
			}
		}

		.popup-btn {
			margin: 0 auto;
			width: 200rpx;
			height: 60rpx;
			border-radius: 10rpx;
			color: #fff;
			font-size: 28rpx;
			letter-spacing: 2px;
			display: flex;
			align-items: center;
			justify-content: center;
			background: #FD4C61;
		}
	}

	.x-list {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		flex-direction: row;
		padding: 0 60rpx;

		.list-cell {
			width: 27%;
			height: 170rpx;
			border: 2rpx solid #fff;
			display: flex;
			align-items: center;
			flex-direction: column;
			padding: 5rpx 0rpx 10rpx;
			border-radius: 16rpx;
			position: relative;

			&.list-active {
				border-radius: 16rpx;
				border: 2rpx solid #70737C;
			}

			.list-btn {
				width: calc(100% + 4rpx);
				height: 52rpx;
				color: #fff;
				background: #FD4C61;
				border-radius: 0rpx 0rpx 16rpx 16rpx;
				position: absolute;
				bottom: -2rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 24rpx;


				.count {
					color: #fff;
					line-height: 60rpx !important;
					// margin-right: 5rpx;

					input {
						width: 40rpx;
						//border: #fff solid 1rpx;
					}
				}

				.add {

					display: flex;
					justify-content: center;
					align-items: center;
					/* border: 2rpx solid #ccc; */
					box-sizing: border-box;
					font-size: 52rpx;
					width: 75rpx;
					color: #fff;
				}
			}

			.list-icon {
				width: 80rpx;
				height: 80rpx;
			}

			.list-name {
				color: #333;
				font-size: 26rpx;
				margin-top: 8rpx;
			}

			.list-price {
				color: #333;
				font-size: 22rpx;
				margin-top: 5rpx;
			}
		}
	}
</style>